06. Looking Ahead at New Operators

Looking Ahead

It's time for some self-directed learning and discovery. In this exercise, we are going to preview some other operators provided in Swift and give you an opportunity to guess what they do.

New Operators Quiz

QUESTION:

Here are some new operators and examples.

  • && operator:
    • var stayAtHome = isWeekend && isRaining
  • ! operator:
    • var wearWarmClothes = !isHot && !stayingInside
  • <= operator:
    • var shouldContinue = numberOfLives <= 0

What do you think these operators do? What kinds of values do they return? Take your best guess or try them out in a Xcode Playground.

ANSWER:

Each of the above operators are closely related to Boolean values. They allow us to compose complex truth statements such as “if it is the weekend and it is raining, then I should stay at home”. Or, “if it is not hot and I am not staying inside, then I should wear warm clothes”. In each case, based on the values of the variables involved, we will get a result true or false value.

The specific names of these operators are…

  • && is the Logical AND operator
  • ! is the **Logical NOT **operator
  • <= is **Less Than or Equal To **operator

Wrap Up

To learn more about these operators see Apple's Basic Operators documentation for Swift and go to the Logical Operators and Comparison Operators sections.

For the final exercise, download the Operators and Expressions Playground from the Beginning iOS Playground Collection and complete the exercises contained within it.

Operators and Expressions Outro